package com.heima.article.feign;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleConfigService;
import com.heima.article.service.ApArticleService;
import com.heima.feign.article.IArtileFeignClient;
import com.heima.model.article.dtos.ArticleCommentDto;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.comment.dtos.ApCommentDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.StatisticsDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
public class ArticleClient implements IArtileFeignClient {


    @Autowired
    private ApArticleService apArticleService;
    @Autowired
    private ApArticleConfigService apArticleConfigService;

    @Override
    @PostMapping("/api/v1/article/save")
    public ResponseResult saveArticle(@RequestBody ArticleDto dto) {

        //测试熔断降级：
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        return apArticleService.saveArticle(dto);
    }

    /**
     * 图文统计统计
     * @param wmUserId
     * @param beginDate
     * @param endDate
     * @return
     */
    @GetMapping("/api/v1/article/queryLikesAndConllections")
    @Override
    public ResponseResult queryLikesAndConllections(@RequestParam("wmUserId") Integer wmUserId,@RequestParam("beginDate") Date beginDate,@RequestParam("endDate") Date endDate) {
        return apArticleService.queryLikesAndConllections(wmUserId,beginDate,endDate);
    }


    @PostMapping("/api/v1/article/newPage")
    @Override
    public PageResponseResult newPage(@RequestBody StatisticsDto dto) {
        return apArticleService.newPage(dto);
    }

    /**
     * 保存mysql文章评论状态
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/article/isChannel")
    public ResponseResult isChannel(@RequestBody ApCommentDto dto){
        return apArticleConfigService.isChannel(dto);
    }

    /**
     * 删除评论数,点赞数
     * @param dto
     * @return
     */
    @PostMapping("/api/v1/article/delChannel")
    public ResponseResult delChannelState(@RequestBody ApCommentDto dto){
        return apArticleService.delChannelState(dto);
    }

    @Override
    @PostMapping("/api/v1/article/findNewsComments")
    public PageResponseResult findNewsComments(@RequestBody ArticleCommentDto dto) {
        return apArticleService.findNewsComments(dto);
    }

    @GetMapping("/api/v1/article/findArticleConfigByArticleId/{articleID}")
    @Override
    public ResponseResult findArticleConfigaByArticleId(@PathVariable("articleID") Long id) {
        ApArticleConfig one = apArticleConfigService.getOne
                (Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, id));
        return ResponseResult.okResult(one);
    }

    /**
     * 保存文章评论数
     * @param entryId
     */
    @GetMapping("/api/v1/article/update/{entryId}")
    public ResponseResult updateComment(@PathVariable("entryId") Long entryId){
        return apArticleService.updateComment(entryId);
    }

    /**
     * 判断该文章是否开启评论
     * @param entryId
     * @return
     */
    @GetMapping("/api/v1/article/state/{entryId}")
    public ResponseResult stateComment(@PathVariable("entryId")Long entryId){
        return apArticleConfigService.stateComment(entryId);
    }
}